import React, { Component } from 'react';
import router from 'umi/router';
import { Toast, Modal, WingBlank, List, Button } from 'antd-mobile';
import styles from './index.less';
import MyNavBar from 'components/navbar';
import { AUTHENTICATION_FAIL_CODE, PROCESS_STATUS, ENTER_TYPE } from 'utils/Enum';
import { connect } from 'dva'
import { checkUrl, getIsDingTalk, backToOut } from 'utils/utils'
import commonStyles from 'utils/common.less';
import { recordEnterHistoryNum } from '../../utils/utils';
@connect(({ PersonalCertificaModel }) => ({ PersonalCertificaModel }))
class signature extends Component {
  constructor(props) {
    super(props)
    this.state = {
      options: [
        {
          id: 'Alipay',
          name: '支付宝认证',
          desc: '本人支付宝已实名，使用支付宝授权认证',
          img: require('../../assets/Alipay.png'),
          url: '/PersonalCertificationAlipayDetail'
        },
        {
          id: 'telphone',
          name: '手机三要素认证',
          desc: '本人手机号已实名，通过运营商认证',
          img: require('../../assets/telphone.png'),
          url: '/PersonalCertificationDetail'
        }
      ]
    }
  }

  async componentDidMount() {
    let { location } = this.props
    let { query } = location
    if (checkUrl(query)) {
      //如果是正常的url
      //如果是从外部跳过来
      // localStorage.setItem('Certification',PROCESS_STATUS.UNKOWN_EXTERNALPUSR_APPLY)
      const Session = localStorage.getItem('Session')
      const url = localStorage.getItem('returnUrl')
      const enterType = localStorage.getItem('enterType')
      if (enterType === ENTER_TYPE.EXTERNAL) {
        //如果获取的Session和存储的Session一致就不需要重新获取
        let { dispatch } = this.props
        // if(!localStorage.getItem('type') || !localStorage.getItem('userWsid')) {
        Toast.loading('查询中')
        await dispatch({
          type: 'PersonalCertificaModel/getUserWsid',
          payload: Session,
          callback: (state, wsid) => {
            if (state === 'success') {
              localStorage.setItem('userWsid', wsid)
              let reg = new RegExp(/.*?_(.*?)_.*?/, 'i')
              let p = reg.exec(wsid)
              localStorage.setItem('type', p[1])
              Toast.hide()
            } else {
              Toast.fail('未登录', 2, () => {
                backToOut();
              })
            }
          }
        })
        // }
      }
    } else {
      if (localStorage.getItem("enterType") !== ENTER_TYPE.INTERIOR) {
        const alert = Modal.alert
        alert('警告', '无法操作！', [
          {
            text: '返回', onPress: () => {
              backToOut()
            }
          },
        ])
      }

    }
    const isEnterPage = localStorage.getItem('isEnterPage') || 'yes';
    if (isEnterPage === 'yes') {
      recordEnterHistoryNum();
    }
  }
  //跳转详情页
  goDetail = (id, go) => {
    let { location: { query: { type = '', url = '', Certification = PROCESS_STATUS.UNKOWN_EXTERNALPUSR_APPLY, } } } = this.props
    url = url && url !== 'undefined' ? url : localStorage.getItem('returnUrl')
    type = type && type !== 'undefined' ? type : localStorage.getItem('type')
    if (Certification == 'undefined' || !Certification) Certification = PROCESS_STATUS.UNKOWN_EXTERNALPUSR_APPLY
    router.push(`${go}?type=${type}&Certification=${Certification}&url=${escape(url)}`)
  }

  notJustPersonal = () => {
    const userAgent = window.location.href.toLowerCase();
    console.log('userA', userAgent)
    return userAgent.indexOf('urlback') > -1 || false;
  }

  render() {
    const isDingTalk = getIsDingTalk();
    const enterType = localStorage.getItem('enterType')
    const isExternal = enterType === ENTER_TYPE.EXTERNAL ? true : false
    const type = localStorage.getItem('type')
    const { options } = this.state
    let url = '';
    const Item = List.Item;
    const Brief = Item.Brief;
    let notJustPersonal = this.notJustPersonal();
    console.log('notJustPersonal', notJustPersonal)
    if (notJustPersonal) {
      url = '/EnterpriseCertificationListBackUps'
    }
    else {
      url = ''
    }

    // const contentContainer=' '+commonStyles.headerTop
    const contentContainer = isDingTalk === 'true' ? '' : ' ' + commonStyles.headerTop
    return (
      <div className={styles.baseLayout}>
        <MyNavBar title={"选择实名认证方式"} isExternal={isExternal} backUrl={url} />
        <WingBlank className={'wingBlankBox ' + contentContainer}>
          {options.map((item, index) => {
            return (
              <List className={styles.List}>
                <Item align="middle" key={index} className={styles.item} onClick={this.goDetail.bind(this, item.id, item.url)} activeStyle={{ background: "url('../../assets/yinmoku.png)" }}
                  thumb={item.img} multipleLine>
                  {item.name} <Brief className={styles.desc}>{item.desc}</Brief>
                  {/*<div className={styles.border}>kdsaopdksda</div>*/}
                </Item>
              </List>
            )
          })}
        </WingBlank>
      </div>
    )
  }
}

export default signature;





